package in.muton.edushare.registration;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareLogin;
import in.muton.edushare.registration.university.UnivInvite;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;

import com.edushare.common.dao.TempUserDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.CryptionUtil;
import com.edushare.common.vo.Users;

public class VerifyRegistration extends EduSharePage {

	private static final long serialVersionUID = -160254699609458982L;
	private static Log log = LogFactory.getLog(VerifyRegistration.class);
			
	public VerifyRegistration(PageParameters params){
		getSession().bind();
		String verificationCode = params.getString("key", "");
		log.debug("verificationCode: "+verificationCode);
		boolean isValid = false;
		try {
			isValid = new TempUserDAO(getSessionORMInterface()).verifyConfKey(verificationCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String userName = null;
		if(isValid){
			try {
				userName = CryptionUtil.decrypt(verificationCode);
			} catch (Exception e) {
				e.printStackTrace();
			}
			userName = userName.substring(0, userName.lastIndexOf('.'));
			/** Now, expecting that the above logic works */
			Users loggedInUser = new UsersDAO(getSessionORMInterface()).getUserFromUserName(userName);
			EduShareSession.get().logInUser(loggedInUser);
			add(new Label("uname", loggedInUser.getCompleteUserName()).setRenderBodyOnly(true).setVisible(true));
			add(new BookmarkablePageLink("firstlogin", UnivInvite.class));
			log.debug("User Registering: "+ loggedInUser.getUserName());
		}
		else{
			log.debug("redirecting to login page from verification page");
			setResponsePage(EduShareLogin.class);
			
		}
			
			
	}

}
